package assignment_service

import (
	"go_api/models/mongodb"
	"gopkg.in/mgo.v2/bson"
	"strings"
)

type Assignment struct {
	ID           	string
	TaskName      	string
	Status       	string
	StudentId		int
	MissionType  	string
	PageNum  		int
	PageSize 		int
}


func (a *Assignment) GetAll() ( assignments []*mongodb.Assignment, err error) {
	assignments, err = mongodb.GetAssignments(a.PageNum, a.PageSize, a.getMaps())
	if err != nil {
		return nil, err
	}

	return assignments, nil
}

func (a *Assignment) Count() (int, error) {
	return mongodb.GetArticleTotal(a.getMaps())
}

func (a *Assignment) getMaps() bson.M {
	match := bson.M{}
	if a.Status != "" {
		s := strings.Split(a.Status, ",")
		match["status"] = bson.M{"$in": s}
	}

	if a.MissionType != "" {
		match["missionType"] = a.MissionType
	}

	if a.StudentId > 0 {
		match["student_id"] = a.StudentId
	}

	return match
}